<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8 0008
 * Time: 16:15
 */
namespace app\api\middleware;

use app\currency\service\AccessTokenService;
use library\Controller;
use think\Db;

/**
 * 用户登陆中间件
 * Class CheckMember
 * @package app\api\middleware
 */
class MemberAuth extends Controller
{
    /**
     * 用户登陆验证中间件
     * @param $request
     * @param \Closure $next
     * @param string $name
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function handle($request, \Closure $next,$name = '')
    {
        // 接受token
        $token = $this->request->header('Authorization',input('server.HTTP_AUTHORIZATION',''));
        $token = trim(substr($token, 6));
        $mid = AccessTokenService::checkAccessToken($token);
        if(is_array($mid) && $name !== 'compatible'){
            $this->error($mid['msg'],'',$mid['code']);
        }
        $member = Db::name('Member')->where('id',$mid)->find();
        if($member['phone'] == '' && $name != 'compatible')
        {
            $this->success('您还未绑定手机号！','',202);
        }
        $request->member = (object)$member;
        $request->mid = $mid;
        return $next($request);
    }
}